﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class UIABPR : ViewBase
    {
        #region ==FTABPRREV===========
        public static readonly DependencyProperty FTABPRREVProperty = DependencyProperty.Register("FTABPRREV",
            typeof(bool), typeof(UIABPR),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTABPRREVChanged));
        public bool FTABPRREV
        {
            get
            {
                return (bool)GetValue(FTABPRREVProperty);
            }
            set
            {
                SetValue(FTABPRREVProperty, value);
            }
        }
        private static void OnFTABPRREVChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.FTABPRREV = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==dwAR===========
        public static readonly DependencyProperty dwARProperty = DependencyProperty.Register("dwAR",
            typeof(DataRow), typeof(UIABPR),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwARChanged));
        public DataRow dwAR
        {
            get
            {
                return (DataRow)GetValue(dwARProperty);
            }
            set
            {
                SetValue(dwARProperty, value);
            }
        }
        private static void OdwARChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwAR = row;
            }
        }
        #endregion 

        #region ==FTABPR===========
        public static readonly DependencyProperty FTABPRProperty = DependencyProperty.Register("FTABPR",
            typeof(bool), typeof(UIABPR),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTABPRChanged));
        public bool FTABPR
        {
            get
            {
                return (bool)GetValue(FTABPRProperty);
            }
            set
            {
                SetValue(FTABPRProperty, value);
            }
        }
        private static void OnFTABPRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.FTABPR = (bool)e.NewValue;
            }
        }
        #endregion
        public VABPR TMV
        {
            get
            {
                return (VABPR)MV;
            }
        }
        public UIABPR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded) return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwAR = dwMaster;
            if (dwMaster == null)
            {
                MWERT1 = 0;
            }
            else
            {
                MWERT1 = TMV.ARMWERT1;
            }
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            Update();
            base.OnSave();
        }
        public override void Update()
        {
            foreach (DataRow row in TBMain.Rows)
            {
                if (row.RowState == DataRowState.Unchanged)
                    continue;
                TMV.UpdateABPR(row);
            }
            base.Update();
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            Update();

            base.SaveCommand(sender, e);
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {

            }
        }
        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM == "")
                return;
            base.LOADBYAUF();
            PM["ORDERNUM"] = ORDERNUM;
            SResult = TMV.LOAD(PM);
            MergeData(SResult, true);
        }

    }
    public partial class UIABPR
    {
        #region ==MWERT1===========
        public static readonly DependencyProperty MWERT1Property = DependencyProperty.Register("MWERT1",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMWERT1Changed));
        public double MWERT1
        {
            get
            {
                return (double)GetValue(MWERT1Property);
            }
            set
            {
                SetValue(MWERT1Property, value);

            }
        }
        private static void OnMWERT1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.MWERT1 = (double)e.NewValue;
                src.OnMWERT1Chg((double)e.NewValue);
            }
        }
        public virtual void OnMWERT1Chg(double value)
        {
            if (TMV == null)
                return; 
            TMV.ARMWERT1 = value; 
            TMV.ARWERT = TMV.ARMWERT1*0.001*0.001*100;
        }
        #endregion
        #region ==MWERT2===========
        public static readonly DependencyProperty MWERT2Property = DependencyProperty.Register("MWERT2",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMWERT2Changed));
        public double MWERT2
        {
            get
            {
                return (double)GetValue(MWERT2Property);
            }
            set
            {
                SetValue(MWERT2Property, value);

            }
        }
        private static void OnMWERT2Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.MWERT2 = (double)e.NewValue;
                src.OnMWERT2Chg((double)e.NewValue);
            }
        }
        public virtual void OnMWERT2Chg(double value)
        {
            if (TMV == null)
                return;
            TMV.ARMWERT2 = value;
         }
        #endregion  
   
        
        #region ==GFDISM===========
        public static readonly DependencyProperty GFDISMProperty = DependencyProperty.Register("GFDISM",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGFDISMChanged));
        public double GFDISM
        {
            get
            {
                return (double)GetValue(GFDISMProperty);
            }
            set
            {
                SetValue(GFDISMProperty, value);
            }
        }
        private static void OnGFDISMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.GFDISM = (double)e.NewValue;
                src.OnGFDISMChg((double)e.NewValue);
            }
        }
        public virtual void OnGFDISMChg(double value)
        {
            OnDPRODChg(value);
        }
        #endregion       

        #region ==DPROD===========
        public static readonly DependencyProperty DPRODProperty = DependencyProperty.Register("DPROD",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDPRODChanged));
        public double DPROD
        {
            get
            {
                return (double)GetValue(DPRODProperty);
            }
            set
            {
                SetValue(DPRODProperty, value);

            }
        }
        private static void OnDPRODChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.DPROD = (double)e.NewValue;
            }
        }
        public virtual void OnDPRODChg(double value)
        {
            MWERT1 = 0;
            if (GFDISM > 0)
            {
                DPROD = Helper.RoundX(SPEED * 14.4 / (GFDISM * 0.01 / 2.54) * EFFICIENCY / 100, 100);
                WVCOST = 0;
                if (DPROD > 0)
                {
                    WVCOST = Helper.RoundX(LOOMCOST / DPROD, 100);

                }
                MWERT1 = WVCOST*1000*1000;
            }
        }
        #endregion

        #region ==SPEED===========
        public static readonly DependencyProperty SPEEDProperty = DependencyProperty.Register("SPEED",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSPEEDChanged));
        public double SPEED
        {
            get
            {
                return (double)GetValue(SPEEDProperty);
            }
            set
            {
                SetValue(SPEEDProperty, value);
                OnDPRODChg(value);
            }
        }
        private static void OnSPEEDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.SPEED = (double)e.NewValue;
            }
        }
        #endregion

        #region ==EFFICIENCY===========
        public static readonly DependencyProperty EFFICIENCYProperty = DependencyProperty.Register("EFFICIENCY",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEFFICIENCYChanged));
        public double EFFICIENCY
        {
            get
            {
                return (double)GetValue(EFFICIENCYProperty);
            }
            set
            {
                SetValue(EFFICIENCYProperty, value);
                OnDPRODChg(value);
            }
        }
        private static void OnEFFICIENCYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.EFFICIENCY = (double)e.NewValue;
            }
        }
        #endregion

        #region ==LOOMCOST===========
        public static readonly DependencyProperty LOOMCOSTProperty = DependencyProperty.Register("LOOMCOST",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLOOMCOSTChanged));
        public double LOOMCOST
        {
            get
            {
                return (double)GetValue(LOOMCOSTProperty);
            }
            set
            {
                SetValue(LOOMCOSTProperty, value);
            }
        }
        private static void OnLOOMCOSTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.LOOMCOST = (double)e.NewValue;
                src.OnLOOMCOSTChg((double)e.NewValue);
            }
        }
        public virtual void OnLOOMCOSTChg(double value)
        {

        }
        #endregion       

        #region ==WVCOST===========
        public static readonly DependencyProperty WVCOSTProperty = DependencyProperty.Register("WVCOST",
        typeof(double), typeof(UIABPR),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWVCOSTChanged));
        public double WVCOST
        {
            get
            {
                return (double)GetValue(WVCOSTProperty);
            }
            set
            {
                SetValue(WVCOSTProperty, value);
            }
        }
        private static void OnWVCOSTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIABPR;
            if (src != null)
            {
                src.WVCOST = (double)e.NewValue;
                src.OnWVCOSTChg((double)e.NewValue);
            }
        }
        public virtual void OnWVCOSTChg(double value)
        {

        }
        #endregion

      
    }
}
